import java.util.HashMap;
import java.util.Map;

public class Solution5 {
    public static void main(String[] args) {
        int[] records = {0};
        int ret = takeAttendance2(records);
        System.out.println(ret);
    }

    //方法一：二分查找 - O(logn) - 高效
    public static int takeAttendance1(int[] records) {
        int left = 0;
        int right = records.length - 1;
        while(left < right) {
            int mid = left + (right - left) / 2;
            if(records[mid] == mid) {
                left = mid + 1;
            }else {
                right = mid;
            }
        }
        if(records[left] == left) {
            return left + 1;
        }
        return left;
    }

    //方法二：哈希表 - O(n) - 低效
    public static int takeAttendance2(int[] records) {
        Map<Integer,Integer> hashMap = new HashMap<>();
        int n = records.length + 1;

        for(int i = 0; i < n - 1; i++) {
            hashMap.put(records[i],1);
        }
        for(int i = 0; i < n; i++) {
            if(hashMap.get(i) == null) {
                return i;
            }
        }
        return n;
    }

    //方法三：异或 - O(n) - 低效
    public static int takeAttendance3(int[] records) {
        int n = records.length + 1;
        int ret = 0;
        for(int i = 0; i < n - 1; i++) {
            ret = ret ^ records[i] ^ (i + 1);
        }
        return ret;
    }

    //方法四：数学(高斯求和公式) - O(n) - 低效
    public static int takeAttendance4(int[] records) {
        int n = records.length + 1;
        long sum = (0 + n - 1) * n / 2;
        for(int i = 0; i < n - 1; i++) {
            sum -= records[i];
        }
        return (int)sum;
    }
}